<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="widget-card" flex>
  <md-card-title class="gravitee-widget-draggable">
    <md-card-title-text ng-if="!$ctrl.isModificationMode || ($ctrl.isModificationMode && !$ctrl.updateMode)">
      <span class="md-title"><strong>{{ $ctrl.widget.title }}</strong></span>
      <span class="md-subhead">{{ $ctrl.widget.subhead }}</span>
    </md-card-title-text>
    <md-card-title-text ng-if="$ctrl.isModificationMode && $ctrl.updateMode">
      <md-input-container>
        <label>Title</label>
        <input type="text" ng-model="$ctrl.widget.title" required />
      </md-input-container>
      <md-input-container>
        <label>Subtitle</label>
        <input type="text" ng-model="$ctrl.widget.subhead" />
      </md-input-container>
    </md-card-title-text>
    <md-card-title-media>
      <a
        aria-label="Modify widget"
        ng-if="$ctrl.updateMode"
        ng-click="$ctrl.isModificationMode=!$ctrl.isModificationMode"
        ng-switch="$ctrl.isModificationMode"
      >
        <ng-md-icon ng-switch-when="true" icon="border_color"></ng-md-icon>
        <ng-md-icon ng-switch-default icon="edit"></ng-md-icon>
      </a>
      <a title="Remove widget" ng-click="$ctrl.delete()"><ng-md-icon icon="close"></ng-md-icon></a>
    </md-card-title-media>
  </md-card-title>

  <md-card-content ng-if="$ctrl.updateMode">
    <gravitee-empty-state ng-if="!$ctrl.isModificationMode" icon="{{$ctrl.getIconFromType()}}" model="Dashboard"></gravitee-empty-state>
    <div class="gv-widget-content" ng-if="!$ctrl.widget.chart.type" layout="column">
      <md-input-container>
        <label>Widget type</label>
        <md-select ng-model="$ctrl.widget.chart.type" ng-change="$ctrl.isModificationMode = true" required>
          <md-option ng-repeat="widgetType in ['table', 'line', 'pie', 'map', 'stats']" ng-value="widgetType">{{widgetType}}</md-option>
        </md-select>
      </md-input-container>
    </div>
    <div ng-if="$ctrl.isModificationMode" ng-switch="$ctrl.widget.chart.type">
      <gv-widget-data-table-configuration ng-switch-when="table" chart="$ctrl.widget.chart"></gv-widget-data-table-configuration>
      <gv-widget-data-line-configuration ng-switch-when="line" chart="$ctrl.widget.chart"></gv-widget-data-line-configuration>
      <gv-widget-data-pie-configuration ng-switch-when="pie" chart="$ctrl.widget.chart"></gv-widget-data-pie-configuration>
      <gv-widget-data-map-configuration ng-switch-when="map" chart="$ctrl.widget.chart"></gv-widget-data-map-configuration>
      <gv-widget-data-stats-configuration ng-switch-when="stats" chart="$ctrl.widget.chart"></gv-widget-data-stats-configuration>
    </div>
  </md-card-content>

  <md-card-content ng-if="!$ctrl.updateMode">
    <div ng-if="$ctrl.fetchData" layout="column" layout-align="center center" flex>
      <md-progress-circular md-mode="indeterminate" class="md-accent md-hue-1" md-diameter="50"></md-progress-circular>
    </div>

    <div style="height: 100%" ng-if="!$ctrl.fetchData && $ctrl.results" ng-switch on="$ctrl.widget.chart.type">
      <gv-widget-data-table
        class="test"
        ng-switch-when="table"
        data="$ctrl.results"
        activated-route="$ctrl.activatedRoute"
      ></gv-widget-data-table>
      <gv-widget-chart-line ng-switch-when="line" data="$ctrl.results" activated-route="$ctrl.activatedRoute"></gv-widget-chart-line>
      <gv-widget-chart-pie ng-switch-when="pie" data="$ctrl.results"></gv-widget-chart-pie>
      <gv-widget-chart-map ng-switch-when="map" data="$ctrl.results"></gv-widget-chart-map>
      <gv-widget-data-stats ng-switch-when="stats" data="$ctrl.results"></gv-widget-data-stats>
      <gv-widget-chart-count ng-switch-when="count" data="$ctrl.results"></gv-widget-chart-count>
    </div>

    <div ng-if="!$ctrl.fetchData && !$ctrl.results" layout="column" layout-align="center center" flex>
      <p><ng-md-icon icon="block" style="fill: grey"></ng-md-icon></p>
      <p>No data</p>
    </div>
  </md-card-content>
</div>
